/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"errors"
	wxcard "github.com/chanxuehong/wechat/mp/card"
	"github.com/chanxuehong/wechat/mp/card/code"
	wxmembercard "github.com/chanxuehong/wechat/mp/card/membercard"
	wxcarduserinfo "github.com/chanxuehong/wechat/mp/card/membercard/userinfo"
	"github.com/chanxuehong/wechat/mp/core"
	"github.com/mjiulee/lego"
)
import . "saas_sys_base/modules/wechat/models"

type WxCardUserMemberCardService struct {
	// add your model here
	tbWxCardUserMemberCard WxCardUserMemberCard
	wechatService          WechatService
}

func (t *WxCardUserMemberCardService) ListWxCardUserMemberCard(keyword string, page, psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_wx_card_user_member_card", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			//{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}
	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *WxCardUserMemberCardService) GetWxCardUserMemberCardById(id int64) *WxCardUserMemberCard {
	return t.tbWxCardUserMemberCard.RecodeById(id)
}

/* 添加
 * -----------------
 */
func (t *WxCardUserMemberCardService) SaveWxCardUserMemberCard(item2add *WxCardUserMemberCard) bool {
	return t.tbWxCardUserMemberCard.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *WxCardUserMemberCardService) DelWxCardUserMemberCardById(id int64) bool {
	return t.tbWxCardUserMemberCard.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *WxCardUserMemberCardService) UpdateWxCardUserMemberCard(rc *WxCardUserMemberCard) bool {
	return t.tbWxCardUserMemberCard.UpdateRecode(rc)
}

func (t *WxCardUserMemberCardService) SubmitMemberCardUserInfoEventHandler(weappid string, mixedMsg *core.MixedMsg) error {
	msg := wxcard.GetSubmitMemberCardUserInfoEvent(mixedMsg)
	//weappid := msg.AppId
	cardid := msg.CardId
	cardcode := msg.UserCardCode
	wechatCli, err := t.wechatService.GetClientByWeappid(weappid)
	if err != nil {
		return err
	}

	cardIdentifier := &code.CardItemIdentifier{
		CardId: cardid,
		Code:   cardcode,
	}

	//查询用户填写会员卡的字段值
	userinfo, err := wxcarduserinfo.Get(wechatCli, cardIdentifier)
	if err != nil {
		return err
	}

	activateParam := &wxmembercard.ActivateParameters{
		CardId: cardid,
		Code:   cardcode,

		MembershipNumber: cardcode, // 必填, 会员卡编号，由开发者填入，作为序列号显示在用户的卡包里。可与Code码保持等值。
	}

	err = wxmembercard.Activate(wechatCli, activateParam)
	if err != nil {
		lego.LogError(err)
		return err
	}
	rc := &WxCardUserMemberCard{
		WeappId:      weappid,
		CardId:       cardid,
		UserCardCode: cardcode,
		CardUserInfo: *userinfo,
	}

	isSuccess := t.tbWxCardUserMemberCard.AddRecode(rc)
	if !isSuccess {
		return errors.New("tbWxCardUserMemberCard.AddRecode failed")
	}

	return nil
}
